package org.linloong.modules.apis.monitor.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Data
@Schema(name = "折线图json数据")
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class LineEchartsDataDTO {

    private String type;

    private String unit;

    private List<LineData> data;

    @Data
    @Schema(name = "对应点数据")
    @AllArgsConstructor
    public static class LineData{
        private String time;
        private String value;
    }

    private static final DateTimeFormatter FORMATTER =
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                    .withZone(ZoneId.systemDefault());

    public LineEchartsDataDTO init(String name, String unit, Integer limit, Long interval){
        List<LineEchartsDataDTO.LineData> lineList = new ArrayList<>(limit);
        for (int i = 0; i < limit; i++) {
            lineList.add(new LineEchartsDataDTO.LineData(
                    LocalDateTime.now().minusMinutes(i * interval).format(FORMATTER),
                    String.valueOf(0)));
        }
        Collections.reverse(lineList);
        return LineEchartsDataDTO.builder()
                .type(name)
                .unit(unit)
                .data(lineList)
                .build();
    }

}
